package com.kusonl.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component(
        value = "myAdvice"
)
@Aspect
public class MyAdvice {

    @Before(value = "execution(* *..add*())")
    public void begin() {
        System.out.println("开启事务");
    }

    @Before(value = "execution(* *..add*())")
    public void commit() {
        System.out.println("提交事务");
    }

    public void after_throwing() {

        System.out.println("\n异常拦截通知.....");
    }

    public void after_returning() {
        System.out.println("after_running ---- 后置通知");
    }

    public Object around(ProceedingJoinPoint pj) throws Throwable {
        System.out.println("around - 开启事务..");
        Object proceed = pj.proceed();
            // 环绕通知 ProceedingJoinPoint 执行proceed方法的作用是让目标方法执行，这也是环绕通知和前置、后置通知方法的一个最大区别。

        System.out.println("around - 提交事务..");
        return proceed;
    }
}
